package com.hckjsec.law.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import com.hckjsec.law.domain.WebLawMechanismDetail;
import com.hckjsec.law.service.IWebLawMechanismDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.law.domain.WebLawMechanism;
import com.hckjsec.law.service.IWebLawMechanismService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公法机构简介主Controller
 * 
 * @author cyt
 * @date 2021-11-03
 */
@RestController
@RequestMapping("/system/lawmechanism")
public class WebLawMechanismController extends BaseController
{
    @Autowired
    private IWebLawMechanismService webLawMechanismService;

    @Autowired
    private IWebLawMechanismDetailService webLawMechanismDetailService;

    /**
     * 获取公法机构简介主详细信息
     */
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        WebLawMechanism webLawMechanism = webLawMechanismService.selectWebLawMechanismById(configId);
        WebLawMechanismDetail webLawMechanismDetail = new WebLawMechanismDetail();
        webLawMechanismDetail.setConfigId(configId);
        List<WebLawMechanismDetail> detailList = webLawMechanismDetailService.selectWebLawMechanismDetailList(webLawMechanismDetail);
        if (!StringUtils.isEmpty(detailList)){
            webLawMechanism.setDetailList(detailList);
        }
        return AjaxResult.success(webLawMechanism);
    }

    /**
     * 新增公法机构简介主
     */
    @Log(title = "公法机构简介主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebLawMechanism webLawMechanism)
    {
        Long configId = webLawMechanism.getConfigId();
        int rows = 0;
        if (!"".equals(StringUtils.isHcNull(webLawMechanism.getContent(),""))){//解密
            webLawMechanism.setContent(Base64Converter.decode(webLawMechanism.getContent()));
        }else{
            webLawMechanism.setContent(webLawMechanism.getContent());
        }
        WebLawMechanism oldWebLawMechanism = webLawMechanismService.selectWebLawMechanismById(configId);
        if (StringUtils.isNull(oldWebLawMechanism)){
            webLawMechanism.setCreateBy(SecurityUtils.getUsername());
            rows = webLawMechanismService.insertWebLawMechanism(webLawMechanism);
        }else{
            webLawMechanism.setUpdateBy(SecurityUtils.getUsername());
            rows = webLawMechanismService.updateWebLawMechanism(webLawMechanism);
        }
        webLawMechanismDetailService.deleteWebLawMechanismDetailById(configId);
        List<WebLawMechanismDetail>  detailList = webLawMechanism.getDetailList();
        if (!StringUtils.isEmpty(detailList)){
            detailList.forEach(kv->{
                WebLawMechanismDetail webLawMechanismDetail = new WebLawMechanismDetail();
                webLawMechanismDetail.setConfigId(configId);
                webLawMechanismDetail.setName(kv.getName());
                webLawMechanismDetail.setPhone(kv.getPhone());
                webLawMechanismDetail.setAddress(kv.getAddress());
                webLawMechanismDetail.setLongitude(kv.getLongitude());
                webLawMechanismDetail.setLatitude(kv.getLatitude());
                webLawMechanismDetailService.insertWebLawMechanismDetail(webLawMechanismDetail);
            });
        }
        return toAjax(rows);
    }
}
